﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.EasyMoveSuiteConfigTemplate;
using BCDto.TMS.EasyMoveSuiteConfigTemplate;
using BCEntity.TMS.EasyMoveSuiteConfigTemplate;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.TMS.VehicleBusinessMapping;
using BCDto.Sim.Admin.Admin;

namespace BCService.TMS.EasyMoveSuiteConfigTemplate
{
	public class EasyMoveSuiteConfigTemplateService : IEasyMoveSuiteConfigTemplateService
	{
		private readonly IRedisService redisService;
		private readonly IEasyMoveSuiteConfigTemplateData easyMoveSuiteConfigTemplateData;
		private readonly IVehicleBusinessMappingData vehicleBusinessMappingData;
		private readonly string key = string.Format("{0}", EntityNameConst.EasyMoveSuiteConfigTemplateEntity);

		public EasyMoveSuiteConfigTemplateService(
			IRedisService redisService,
			IEasyMoveSuiteConfigTemplateData easyMoveSuiteConfigTemplateData,
			IVehicleBusinessMappingData vehicleBusinessMappingData)
		{
			this.redisService = redisService;
			this.easyMoveSuiteConfigTemplateData = easyMoveSuiteConfigTemplateData;
			this.vehicleBusinessMappingData = vehicleBusinessMappingData;
		}

		#region 检查便捷搬家套餐配置模板是否存在
		public bool Exists(int easyMoveSuiteConfigTemplateId)
		{
			return this.easyMoveSuiteConfigTemplateData.Exists(easyMoveSuiteConfigTemplateId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加便捷搬家套餐配置模板
		public EasyMoveSuiteConfigTemplateDto Add(AdminDto adminDto,EasyMoveSuiteConfigTemplateRequestDto requestDto)
		{
			if (adminDto.AgentRecord != null)
			{
				throw new ArgumentException("代理商无权配置此模式模板");
			}
			if (easyMoveSuiteConfigTemplateData.Exists(requestDto.VehicleBusinessMappingId, requestDto.SuiteName).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此套餐配置已存在!");
			}
			var vehicleTransportBusinessConfigEntity = this.vehicleBusinessMappingData.GetEntity(requestDto.VehicleBusinessMappingId).Result;
			if (vehicleTransportBusinessConfigEntity == null)
			{
				throw new ArgumentException("车辆与业务配置信息不存在!");
			}

			var entity = requestDto.As<EasyMoveSuiteConfigTemplateEntity>();
			entity.CreateAdminName = adminDto.FullName;
			entity.CreateAdminId = adminDto.AdminId;
			entity.VehicleTypeId = vehicleTransportBusinessConfigEntity.VehicleTypeId;

			return this.easyMoveSuiteConfigTemplateData.Add(entity).GetAwaiter().GetResult().As<EasyMoveSuiteConfigTemplateDto>();
		}
		#endregion

		#region 修改便捷搬家套餐配置模板
		public EasyMoveSuiteConfigTemplateDto Update(AdminDto adminDto, EasyMoveSuiteConfigTemplatePutDto putDto)
		{
			if (!this.Exists(putDto.EasyMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			if (adminDto.AgentRecord != null)
			{
				throw new ArgumentException("代理商无权配置此模式模板");
			}
			if (easyMoveSuiteConfigTemplateData.Exists(putDto.VehicleBusinessMappingId, putDto.SuiteName).GetAwaiter().GetResult())
			{
				var oldEntity = easyMoveSuiteConfigTemplateData.GetEntity(putDto.VehicleBusinessMappingId, putDto.SuiteName).GetAwaiter().GetResult();
				if (oldEntity.EasyMoveSuiteConfigTemplateId == putDto.EasyMoveSuiteConfigTemplateId)
				{
					throw new ArgumentException("此套餐配置已存在!");
				}
			}
			var vehicleTransportBusinessConfigEntity = this.vehicleBusinessMappingData.GetEntity(putDto.VehicleBusinessMappingId).Result;
			if (vehicleTransportBusinessConfigEntity == null)
			{
				throw new ArgumentException("车辆与业务配置信息不存在!");
			}

			var entity = putDto.As<EasyMoveSuiteConfigTemplateEntity>();
			entity.EditAdminName = adminDto.FullName;
			entity.EditAdminId = adminDto.AdminId;
			entity.VehicleTypeId = vehicleTransportBusinessConfigEntity.VehicleTypeId;

			var result = this.easyMoveSuiteConfigTemplateData.Update(entity).GetAwaiter().GetResult().As<EasyMoveSuiteConfigTemplateDto>();
			string cacheKey = this.redisService.GetKey(key, entity.EasyMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除便捷搬家套餐配置模板
		public bool Delete(int easyMoveSuiteConfigTemplateId)
		{
			if (!this.Exists(easyMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.easyMoveSuiteConfigTemplateData.Delete(easyMoveSuiteConfigTemplateId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, easyMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取便捷搬家套餐配置模板
		public EasyMoveSuiteConfigTemplateDto Get(int easyMoveSuiteConfigTemplateId)
		{
			string cacheKey = this.redisService.GetKey(key, easyMoveSuiteConfigTemplateId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.easyMoveSuiteConfigTemplateData.GetEntity(easyMoveSuiteConfigTemplateId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<EasyMoveSuiteConfigTemplateDto>();
		}
		#endregion

		#region 获取便捷搬家套餐配置模板列表
		public IEnumerable<EasyMoveSuiteConfigTemplateDto> GetList()
		{
			var result = this.redisService.TryGet(key, () =>
			{
				return this.easyMoveSuiteConfigTemplateData.GetList().GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<EasyMoveSuiteConfigTemplateDto>>();
		}
		#endregion

		#region 获取便捷搬家套餐配置模板分页
		public PageableList<EasyMoveSuiteConfigTemplateDto> Query(string suiteName,bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, suiteName, enabled, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.easyMoveSuiteConfigTemplateData.Query(suiteName, enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<EasyMoveSuiteConfigTemplateDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<EasyMoveSuiteConfigTemplateDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion

		#region 启用便捷搬家套餐配置模板
		public bool Enabled(AdminDto adminDto, int easyMoveSuiteConfigTemplateId)
		{
			if (!this.Exists(easyMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.easyMoveSuiteConfigTemplateData.Enabled(easyMoveSuiteConfigTemplateId, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, easyMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 禁用便捷搬家套餐配置模板
		public bool Disabled(AdminDto adminDto, int easyMoveSuiteConfigTemplateId)
		{
			if (!this.Exists(easyMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.easyMoveSuiteConfigTemplateData.Disabled(easyMoveSuiteConfigTemplateId, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, easyMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion
	}
}